package com.example.service;

import com.example.entity.Results;
import com.example.exception.CustomException;
import com.example.mapper.ResultMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ResultService {

    @Resource
    private ResultMapper resultMapper;

    public List<Results> selectAll(Results results) {
        return resultMapper.selectAll(results);
    }

    //分页查询
    public PageInfo<Results> selectPage(Results results, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Results> list = resultMapper.selectAll(results);
        return PageInfo.of(list);
    }

    public void add(Results results) {
        resultMapper.insert(results);
    }

    public void update(Results results) {
        resultMapper.update(results);
    }

    public void delete(Integer id) {
        resultMapper.delete(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            resultMapper.delete(id);
        }
    }


    public Results login(Results results) {
        String studentName = results.getStudentName();
        Results dbstudent = resultMapper.selectByName(studentName);
        if(dbstudent == null) {
            throw new CustomException("500","名字错误");
        }
        return dbstudent;
    }
}
